package com.example.yunxiappbackend.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.yunxiappbackend.pojo.Administrator;
import com.example.yunxiappbackend.pojo.User;
import com.example.yunxiappbackend.service.AdministratorService;
import com.example.yunxiappbackend.service.UserService;
import com.example.yunxiappbackend.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;

/**
 * 登录权限控制
 * 用户未登录时的各种请求控制
 * 计划下面请求都不经过拦截器
 */
@Slf4j
@Controller
public class LoginController {
    @Autowired
    UserService userService;
    @Autowired
    AdministratorService administratorService;

    @RequestMapping("/")
    public String userPage() {
        return "userLogin";
    }

    //普通用户登录
    @ResponseBody
    @RequestMapping("/userLogin")
    public Result<String> userLogin(@RequestBody User user, HttpServletRequest request) {
        if(user!=null && user.getUsername().length()>0 && user.getPassword().length()>0){
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<User>();
            queryWrapper.eq(User::getStatus,1).eq(User::getUsername,user.getUsername());
            User one = userService.getOne(queryWrapper);
            if(one!=null && one.getPassword().equals(user.getPassword())){
                request.getSession().setAttribute("user",user.getUsername());
                return Result.success("登陆成功");
            }else {
                return Result.error("用户名或密码错误");
            }
        }
        return Result.error("用户名或密码不能为空");
    }

    //用户退出登录
    @RequestMapping("/userLogout")
    public String userLogout(HttpServletRequest request) {
        request.getSession().removeAttribute("user");
        return "userLogin";
    }

    @RequestMapping("/toUserRegister")
    public String toRegister(){
        return "userRegister";
    }

    //用户注册
    @ResponseBody
    @RequestMapping("/userRegister")
    public Result<String> userRegister(@RequestBody User user){
        log.info(user.toString());
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<User>();
        queryWrapper.eq(User::getUsername,user.getUsername());
        User one = userService.getOne(queryWrapper);
        if (one == null) {
            userService.save(user);
            return Result.success("注册成功");
        }else {
            return Result.error("用户名已注册");
        }
    }

    @RequestMapping("/background")
    public String background(){
        return "backgroundLogin";
    }

    @RequestMapping("/backgroundLogin")
    @ResponseBody
    public Result<String> backgroundLogin(@RequestBody Administrator administrator,HttpServletRequest request) {
        System.out.println(administrator);
        if (administrator==null){
            return Result.error("不要输入空字符串");
        }
        //检查管理员身份
        LambdaQueryWrapper<Administrator> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Administrator::getAdministratorUsername,administrator.getAdministratorUsername());
        Administrator one = administratorService.getOne(queryWrapper);
        System.out.println(one);
        if (administrator.equals(one)){
            request.getSession().setAttribute("administrator",administrator.getAdministratorUsername());
            return Result.success("登陆成功");
        }else {
            return Result.error("用户名或密码错误");
        }
    }

    @RequestMapping("/mainPage")
    public String mainPage(){
        return "mainPage";
    }

    @RequestMapping("/errorpage")
    public String error() {
        return "sessionTimeout";
    }
}
